package com.zengdada.authority.config.security;

import com.zengdada.authority.config.security.intercept.MyAbstractSecurityInterceptor;
import com.zengdada.authority.config.security.intercept.MyAccessDeniedHandler;
import com.zengdada.authority.config.security.intercept.MyLoginUrlAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyAbstractSecurityInterceptor myAbstractSecurityInterceptor;

    @Value("${web.config.authority.user.login.page.url:/authority/user/login.html}")
    private String loginFormUrl;
    @Value("${web.config.authority.user.login.remember:true}")
    private String remember;
    @Value("${web.config.authority.no.authority.url:}")
    private String noAuthorityUrl;

    private static final String[] STATIC_RES = {"/**/*.js*", "/**/*.css*", "/**/*.img*", "/**/*.png*", "/**/*.ico*", "/**/*.gif*", "/**/*.ttf*", "/**/*.woff*"};

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public MyAccessDeniedHandler myAccessDeniedHandler() throws Exception {
        return new MyAccessDeniedHandler(noAuthorityUrl);
    }

    @Bean
    public MyLoginUrlAuthenticationEntryPoint myLoginUrlAuthenticationEntryPoint() throws Exception {
        return new MyLoginUrlAuthenticationEntryPoint(loginFormUrl, Boolean.parseBoolean(remember));
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http.headers().frameOptions().disable();
        http.csrf().requireCsrfProtectionMatcher(new CsrfSecurityRequestMatcher()).and() // csrf 拦截
                .addFilterBefore(myAbstractSecurityInterceptor, FilterSecurityInterceptor.class)//添加我们自定义的过滤器
//                .authorizeRequests().antMatchers(loginFormUrl, noAuthorityUrl).permitAll()
//                .anyRequest().authenticated() //表示其他资源需要权限
//                .and()
//                .formLogin().loginPage("/user/login.html").and() // 登陆页面地址
//                .logout().invalidateHttpSession(true).logoutSuccessUrl("/user/logout.do").and()//.deleteCookies();
        ;
        http.exceptionHandling()
                .authenticationEntryPoint(myLoginUrlAuthenticationEntryPoint()) //自定义登录地址
                .accessDeniedHandler(myAccessDeniedHandler()); //自定义没有权限时的处理
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
//        super.configure(web);
        web.ignoring().antMatchers(STATIC_RES); //静态资源不拦截
    }
}
